home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / BBEdit / MacBob 1.0ß2 / Source / Utils.c < prev   
Encoding:
C/C++ Source or Header  |  1995-12-05  |  6.6 KB  |  443 lines  |  [TEXT/KAHL]

  1. /***
  2.   *
  3.   *    Utils.c - String and memory utility functions
  4.   *
  5.   *    Copyright (c) 1991, 1994 Symantec Corporation.  All rights reserved.
  6.   *    Edited by Christopher E. Hyde, 1995
  7.   *
  8.   ***/
  9.  
  10.  
  11. /*
  12.  *  ctype.c
  13.  *
  14.  *  Copyright (c) 1991 Symantec Corporation.  All rights reserved.
  15.  *
  16.  */
  17.  
  18. #include <ctype.h>
  19.  
  20.  
  21. int
  22. toupper(int c)
  23. {
  24.     return(islower(c) ? (c ^ 0x20) : c);
  25. }
  26.  
  27.  
  28. int
  29. tolower(int c)
  30. {
  31.     return(isupper(c) ? (c ^ 0x20) : c);
  32. }
  33.  
  34.  
  35. #define __UPPX            (__XDIG|__UPPR)
  36. #define __LOWX            (__XDIG|__LOWR)
  37.  
  38. char __ctype[256] = {
  39.     __CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,
  40.     __CNTL,__WHIT,__WHIT,__WHIT,__WHIT,__WHIT,__CNTL,__CNTL,
  41.     __CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,
  42.     __CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,__CNTL,
  43.     __SPAC,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,
  44.     __PUNC,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,
  45.     __DIGT,__DIGT,__DIGT,__DIGT,__DIGT,__DIGT,__DIGT,__DIGT,
  46.     __DIGT,__DIGT,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,
  47.     __PUNC,__UPPX,__UPPX,__UPPX,__UPPX,__UPPX,__UPPX,__UPPR,
  48.     __UPPR,__UPPR,__UPPR,__UPPR,__UPPR,__UPPR,__UPPR,__UPPR,
  49.     __UPPR,__UPPR,__UPPR,__UPPR,__UPPR,__UPPR,__UPPR,__UPPR,
  50.     __UPPR,__UPPR,__UPPR,__PUNC,__PUNC,__PUNC,__PUNC,__PUNC,
  51.     __PUNC,__LOWX,__LOWX,__LOWX,__LOWX,__LOWX,__LOWX,__LOWR,
  52.     __LOWR,__LOWR,__LOWR,__LOWR,__LOWR,__LOWR,__LOWR,__LOWR,
  53.     __LOWR,__LOWR,__LOWR,__LOWR,__LOWR,__LOWR,__LOWR,__LOWR,
  54.     __LOWR,__LOWR,__LOWR,__PUNC,__PUNC,__PUNC,__PUNC,__CNTL,
  55. };
  56.  
  57.  
  58. /*
  59.  *  str.c
  60.  *
  61.  *  Copyright (c) 1991 Symantec Corporation.  All rights reserved.
  62.  *
  63.  */
  64.  
  65. #pragma options(!require_protos)
  66. #if !__powerc
  67. #include <size_t.h>
  68. #else
  69. #include "string.h"
  70. #endif
  71.  
  72. #if !__powerc
  73. char *
  74. strcpy(/* char *s1, const char *s2 */)
  75. {
  76.     asm {
  77.         movea.l    4(sp),a0        ;  A0 = s1
  78.         movea.l    8(sp),a1        ;  A1 = s2
  79.         move.l    a0,d0            ;  D0.L = result
  80. @1        move.b    (a1)+,(a0)+
  81.         bne.s    @1
  82.     }
  83. }
  84. #else
  85. char *
  86. strcpy(char *s1, const char *s2)
  87. {
  88.     char *ret = s1;
  89.     
  90.     while ((*s1++ = *s2++) != 0)
  91.         ;
  92.  
  93.     return ret;
  94. }
  95. #endif
  96.  
  97.  
  98. #if !__powerc
  99. int
  100. strcmp(/* const char *s1, const char *s2 */)
  101. {
  102.     asm {
  103.         movea.l    4(sp),a0        ;  A0 = s1
  104.         movea.l    8(sp),a1        ;  A1 = s2
  105.         moveq    #0,d0            ;  D0.L = result
  106.         bra.s    @2
  107. @1        tst.b    d1
  108.         beq.s    @4
  109. @2        move.b    (a0)+,d1
  110.         cmp.b    (a1)+,d1
  111.         beq.s    @1
  112.         bhi.s    @3
  113.         subq.l    #2,d0
  114. @3        addq.l    #1,d0
  115. @4    }
  116. }
  117. #else
  118. int
  119. strcmp(const char *s1, const char *s2)
  120. {
  121.     char c1, c2, dif;
  122.     
  123.     for (;;) {
  124.         if (!(c1 = *s1++))
  125.             return *s2 ? -1 : 0;
  126.         if (!(c2 = *s2++))
  127.             return 1;
  128.         if (!(dif = (c1 - c2)))
  129.             continue;
  130.         if (dif < 0)
  131.             return -1;
  132.         else
  133.             return 1;
  134.     }
  135.  
  136.     return 0;
  137. }
  138. #endif
  139.  
  140.  
  141. #if !__powerc
  142. char *
  143. strchr(/* const char *s, int c */)
  144. {
  145.     asm {
  146.         movea.l    4(sp),a0        ;  A0 = s
  147. #if __option(int_4)
  148.         move.b    11(sp),d1        ;  D1.B = (char) c
  149. #else
  150.         move.b    9(sp),d1        ;  D1.B = (char) c
  151. #endif
  152.         moveq    #0,d0            ;  D0.L = result
  153.         bra.s    @2
  154. @1        tst.b    (a0)+
  155.         beq.s    @3
  156. @2        cmp.b    (a0),d1
  157.         bne.s    @1
  158.         move.l    a0,d0
  159. @3    }
  160. }
  161. #else
  162. char *
  163. strchr(const char *s, int c)
  164. {
  165.     do {
  166.         if (*s == c)
  167.             return (char *)s;
  168.     } while (*s++);
  169.     
  170.     return 0;
  171. }
  172. #endif
  173.  
  174.  
  175. #if !__powerc
  176. char *
  177. strpbrk(/* const char *s1, const char *s2 */)
  178. {
  179.     asm {
  180.         movea.l    4(sp),a0        ;  A0 = s1
  181.         moveq    #0,d0            ;  D0.L = result
  182. @1        move.b    (a0)+,d1
  183.         beq.s    @3
  184.         movea.l    8(sp),a1        ;  A1 = s2
  185. @2        move.b    (a1)+,d2
  186.         beq.s    @1
  187.         cmp.b    d1,d2
  188.         bne.s    @2
  189.         subq.l    #1,a0
  190.         move.l    a0,d0
  191. @3    }
  192. }
  193. #else
  194. char *
  195. strpbrk(const char *s1, const char *s2)
  196. {
  197.     const char *t;
  198.     char c;
  199.     
  200.     while ((c = *s1++) != 0) {
  201.         for (t = s2; *t; ++t)
  202.             if (c == *t)
  203.                 return (char *)(s1 - 1);
  204.     }
  205.  
  206.     return 0;
  207. }
  208. #endif
  209.  
  210.  
  211. #if !__powerc
  212. char *
  213. strrchr(/* const char *s, int c */)
  214. {
  215.     asm {
  216.         movea.l    4(sp),a0        ;  A0 = s
  217. #if __option(int_4)
  218.         move.b    11(sp),d1        ;  D1.B = (char) c
  219. #else
  220.         move.b    9(sp),d1        ;  D1.B = (char) c
  221. #endif
  222.         moveq    #0,d0            ;  D0.L = result
  223. @1        cmp.b    (a0),d1
  224.         bne.s    @2
  225.         move.l    a0,d0
  226. @2        tst.b    (a0)+
  227.         bne.s    @1
  228.     }
  229. }
  230. #else
  231. char *
  232. strrchr(const char *s, int c)
  233. {
  234.     const char *res = NULL;
  235.     
  236.     do {
  237.         if (*s == c)
  238.             res = s;
  239.     } while (*s++);
  240.  
  241.     return (char *)res;
  242. }
  243. #endif
  244.  
  245.  
  246. #if !__powerc
  247. size_t
  248. strlen(/* const char *s */)
  249. {
  250.     asm {
  251.         moveq    #-1,d0            ;  D0.L = result
  252.         movea.l    4(sp),a0        ;  A0 = s
  253. @1        addq.l    #1,d0
  254.         tst.b    (a0)+
  255.         bne.s    @1
  256.     }
  257. }
  258. #else
  259. size_t
  260. strlen(const char *s)
  261. {
  262.     size_t len = 0;
  263.     
  264.     while (*s++)
  265.         ++len;
  266.     
  267.     return len;
  268. }
  269. #endif
  270.  
  271.  
  272. /*
  273.  *  strn.c
  274.  *
  275.  *  Copyright (c) 1994 Symantec Corporation.  All rights reserved.
  276.  *
  277.  */
  278.  
  279. #if !__powerc
  280. #pragma options(!require_protos)
  281. #else
  282. #include "string.h"
  283. #endif
  284.  
  285.  
  286. #if !__powerc
  287. char *
  288. strncpy(/* char *s1, const char *s2, size_t n */)
  289. {
  290.     asm {
  291.         move.l    4(sp),d0        ;  D0.L = result
  292.         movea.l    d0,a0            ;  A0 = s1
  293.         movea.l    8(sp),a1        ;  A1 = s2
  294.         move.l    12(sp),d1        ;  D1.L = n
  295.         beq.s    @3
  296. @1        move.b    (a1),(a0)+
  297.         beq.s    @2
  298.         addq.l    #1,a1
  299. @2        subq.l    #1,d1
  300.         bne.s    @1
  301. @3    }
  302. }
  303. #else
  304. char *
  305. strncpy(char *s1, const char *s2, size_t n)
  306. {
  307.     char *res = s1;
  308.     
  309.     while (n--) {
  310.         if ((*s1++ = *s2)!=0)
  311.             ++s2;
  312.     }
  313.     return res;
  314. }
  315. #endif
  316.  
  317.  
  318. #if !__powerc
  319. int
  320. strncmp(/* const char *s1, const char *s2, size_t n */)
  321. {
  322.     asm {
  323.         moveq    #0,d0            ;  D0.L = result
  324.         movea.l    4(sp),a0        ;  A0 = s1
  325.         movea.l    8(sp),a1        ;  A1 = s2
  326.         move.l    12(sp),d1        ;  D1.L = n
  327.         bra.s    @2
  328. @1        tst.b    d2
  329.         beq.s    @4
  330.         subq.l    #1,d1
  331. @2        beq.s    @4
  332.         move.b    (a0)+,d2
  333.         cmp.b    (a1)+,d2
  334.         beq.s    @1
  335.         bhi.s    @3
  336.         subq.l    #2,d0
  337. @3        addq.l    #1,d0
  338. @4    }
  339. }
  340. #else
  341. int
  342. strncmp(const char *s1, const char *s2, size_t n)
  343. {
  344.     char c1, c2, dif;
  345.     
  346.     while (n--) {
  347.         if (!(c1 = *s1++))
  348.             return *s2 ? -11 : 0;
  349.         if (!(c2 = *s2++))
  350.             return 1;
  351.         if (!(dif = (c1 - c2)))
  352.             continue;
  353.         if (dif < 0)
  354.             return -1;
  355.         else
  356.             return 1;
  357.     }
  358.     return 0;
  359. }
  360. #endif
  361.  
  362.  
  363. /*
  364.  *  mem.c
  365.  *
  366.  *  Copyright (c) 1991 Symantec Corporation.  All rights reserved.
  367.  *
  368.  */
  369.  
  370. #pragma options(!require_protos)
  371.  
  372.  
  373. #include <size_t.h>
  374.  
  375. #if !__powerc
  376. void *
  377. memcpy(/* void *s1, const void *s2, size_t n */)
  378. {
  379.     asm {
  380.         move.l    4(sp),d0        ;  D0.L = result
  381.         movea.l    d0,a0            ;  A0 = s1
  382.         movea.l    8(sp),a1        ;  A1 = s2
  383.         move.l    12(sp),d1        ;  D1.L = n
  384.         bra.s    @2
  385. @1        move.b    (a1)+,(a0)+
  386.         subq.l    #1,d1
  387. @2        bne.s    @1
  388.     }
  389. }
  390. #else
  391. void *
  392. memcpy(void *s1, const void *s2, size_t n)
  393. {
  394.     const char *from = (char *)s2;
  395.     char *to = (char *)s1;
  396.     
  397.     while (n--) {
  398.         *to++ = *from++;
  399.     }
  400.  
  401.     return s1;
  402. }
  403. #endif
  404.  
  405.  
  406. #if !__powerc
  407. void *
  408. memchr(/* const void *s, int c, size_t n */)
  409. {
  410.     asm {
  411.         moveq    #1,d0            ;  D0.L = result
  412.         movea.l    4(sp),a0        ;  A0 = s
  413. #if __option(int_4)
  414.         move.b    11(sp),d1        ;  D1.B = (unsigned char) c
  415.         move.l    12(sp),d2        ;  D2.L = n
  416. #else
  417.         move.b    9(sp),d1        ;  D1.B = (unsigned char) c
  418.         move.l    10(sp),d2        ;  D2.L = n
  419. #endif
  420.         bra.s    @2
  421. @1        subq.l    #1,d2
  422. @2        beq.s    @3
  423.         cmp.b    (a0)+,d1
  424.         bne.s    @1
  425.         move.l    a0,d0
  426. @3        subq.l    #1,d0
  427.     }
  428. }
  429. #else
  430. void *
  431. memchr(const void *_s, int c, size_t n)
  432. {
  433.     const char *s = (const char *)_s;
  434.     char *res = (char *)1;
  435.  
  436.     while (n--)
  437.         if (*s++ == c)
  438.             return (void *)(s - 1);
  439.             
  440.     return 0;
  441. }
  442. #endif
  443.